/*
 * @Author: 0x9DEFA478
 * @Date: 2022-02-14 22:08:03
 * @LastEditTime: 2022-08-21 14:33:44
 * @LastEditors: 0x9DEFA478
 * @Description: 
 * QQ:2652450237
 * ============================================================================================================================================
 * 
 * 
 * 
 *                                                                                               ************      ****************************
 *                                                                                             ************      ****************************  
 *                                                                                           ************      ****************************    
 *                                                                                         ************      ****************************      
 *                                                                                       ************      ************                        
 *                                                                                     ************      ************                          
 *                                                                                   ************      ************                            
 *                                                                                 ************      ************                              
 *                                                                               ************      ************                                
 *                                                                             ************      ************                                  
 *                                                                           ************      ************                                    
 *                                                                         ************      ************                                      
 *                                                                       ************      ************                                        
 *                                                                     ************      ************                                          
 *                                                                   ************      ************                                            
 *                                                                 ************      ************                                              
 *                                                               ************      ************                                                
 *                                                             ************      ************                                                  
 *                                                           ************      ************                                                    
 *                                                         ************      ************                                                      
 *                                                       ************      ************                                                        
 *                                                     ************      ************                                                          
 *                                                   ************      ************                                                            
 *                                                 ************      ************                                                              
 *                                               ************      ************                                                                
 *                                             ************      ************                                                                  
 *                                           ************      ************                                                                    
 *                                         ************      ************                                                                      
 *                                       ************      ************                                                                        
 *                                     ************      ************                                                                          
 *                                   ************      ************                                                                            
 *                                 ************      ************                                                                              
 *                               ************      ************                                                                                
 *                             ************      ************                                                                                  
 *                           ************      ************                                                                                    
 *                         ************      ************                                                                                      
 *       ****************************      ************                                                                                        
 *     ****************************      ************                                                                                          
 *   ****************************      ************                                                                                            
 * ****************************      ************                                                                                              
 * 
 * 
 * 
 * ============================================================================================================================================
 * 
 */
#include "../H_NES_Mapper.h"




#define ThisMapper Cartridge.Mapper.SpecificMapper.Mapper21




void H_NES_Mapper21_PRG_Write(Hbyte Value,Huint16 Address,void* v){
  H_NES_Console* Console;
  Hbyte* p;

  Console=v;

  if(Address<0x8000U){
    if(Address<0x6000U){
      H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionPRGWriteAccess,Address);
      // for(;;){
      // }
      return;
    }

    if((Console->ThisMapper.Reg_9002&0x01U)==0U){
      return;
    }

    p=Console->Cartridge.Mapper.PRG_RAM;

    if(p==NULL){
      H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionPRGRAMWriteAccess,Address);
      // for(;;){
      // }
      return;
    }

    p[Address-0x6000U]=Value;
  }else{
    extern void H_NES_Mapper21_PRG_Write_Ext(Hbyte Value,Huint16 Address,H_NES_Console* Console);
    H_NES_Mapper21_PRG_Write_Ext(Value,Address,Console);
  }
}
Hbyte H_NES_Mapper21_PRG_Read(Huint16 Address,void* v){
  H_NES_Console* Console;
  Hbyte* p;

  Console=v;

  if(Address<0x8000U){
    if(Address<0x6000U){
      H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_ReadFail,H_NES_ConstString_ExceptionPRGReadAccess,Address);
      // for(;;){
      // }
      return Address>>8;
    }

    if((Console->ThisMapper.Reg_9002&0x01U)==0U){
      return Address>>8;
    }

    p=Console->Cartridge.Mapper.PRG_RAM;

    if(p==NULL){
      H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_ReadFail,H_NES_ConstString_ExceptionPRGRAMReadAccess,Address);
      // for(;;){
      // }
      return Address>>8;
    }

    return p[Address-0x6000U];
  }else{
    p=Console->ThisMapper.PRG_Banks.Base8000_8KB[(Address>>13)&0x0003U];
    
    return p[Address&0x1FFFU];
  }
}
Huint16 H_NES_Mapper21_PRG_Read16(Huint16 Address,void* v){
  H_NES_Console* Console;
  Hbyte* p;
  Huint16 address;

  Console=v;

  if(Address<0x8000U){
    if(Address<0x6000U){
      H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_ReadFail,H_NES_ConstString_ExceptionPRGReadAccess,Address);
      // for(;;){
      // }
      return (Address&0xFF00U)|(Address>>8);
    }

    if((Console->ThisMapper.Reg_9002&0x01U)==0U){
      return (Address&0xFF00U)|(Address>>8);
    }

    p=Console->Cartridge.Mapper.PRG_RAM;

    if(p==NULL){
      // H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_ReadFail,H_NES_ConstString_ExceptionPRGRAMReadAccess,Address);
      // for(;;){
      // }
      return (Address&0xFF00U)|(Address>>8);
    }

    p=&p[Address-0x6000U];

    if(Address==0x7FFFU){
      address=Address+0x0001U;
      return ((Huint16)p[0])|(((Huint16)H_NES_Mapper21_PRG_Read(address,Console))<<8);
    }

    return ((Huint16)p[0])|(((Huint16)p[1])<<8);
  }else{
    p=Console->ThisMapper.PRG_Banks.Base8000_8KB[(Address>>13)&0x0003U];

    address=Address&0x1FFFU;
    p=&p[address];

    if(address==0x1FFFU){
      address=Address+0x0001U;
      return ((Huint16)p[0])|(((Huint16)H_NES_Mapper21_PRG_Read(address,Console))<<8);
    }
    
    return ((Huint16)p[0])|(((Huint16)p[1])<<8);
  }
}




void H_NES_Mapper21_CHR_Exception_Write(Hbyte Value,Huint16 Address,void* v){
  H_NES_Console* Console;

  Console=v;
  
  H_NES_Mapper_Warning(Console,vH_NES_Result_PRG_WriteFail,H_NES_ConstString_ExceptionCHRWriteAccess,Address);
}
void H_NES_Mapper21_CHR_Write(Hbyte Value,Huint16 Address,void* v){
  H_NES_Console* Console;

  Console=v;

  Console->ThisMapper.CHR_Banks.Base0000_1KB[(Address>>10)&0x0007U][Address&0x03FFU]=Value;
}
Hbyte H_NES_Mapper21_CHR_Read(Huint16 Address,void* v){
  H_NES_Console* Console;

  Console=v;

  return Console->ThisMapper.CHR_Banks.Base0000_1KB[(Address>>10)&0x0007U][Address&0x03FFU];
}

void H_NES_Mapper21_CPUTick(int CPUCycle,void* v){
  H_NES_Console* Console;
  Hbyte Reg_F002;
  Hint32 DivCount;
  Hbyte Count;

  Console=v;

  Reg_F002=Console->ThisMapper.Reg_F002;

  if((Reg_F002&0x02U)!=0U){
    if((Reg_F002&0x40U)==0U){
      DivCount=Console->ThisMapper.DivCount;
      DivCount-=3*CPUCycle;
      if(DivCount>0){
        Console->ThisMapper.DivCount=DivCount;
        return;
      }else{
        Console->ThisMapper.DivCount=DivCount+341;
      }
    }
    Count=Console->ThisMapper.Count;
    if(Count!=0xFFU){
      Console->ThisMapper.Count=Count+0x01U;
    }else{
      Console->ThisMapper.Count=Console->ThisMapper.CountReload;
      Console->CPU.Status|=vH_NES_CPU_Status_IRQ_ExtIRQ;
    }
  }

}




#undef ThisMapper



